package com.wn.icloud.provider.uac.controller;

import com.wn.icloud.common.msg.TableResultResponse;
import com.wn.icloud.provider.uac.service.MenuBiz;
import com.wn.icloud.provider.uac.service.UserBiz;
import com.wn.icloud.provider.uac.entity.Menu;
import com.wn.icloud.provider.uac.entity.User;
import com.wn.icloud.provider.uac.service.PermissionService;
import com.wn.icloud.provider.uac.service.dto.PasswordResetDTO;
import com.wn.icloud.provider.uac.service.dto.UserByWorkTeamReqDTO;
import com.wn.icloud.provider.uac.vo.FrontUser;
import com.wn.icloud.provider.uac.vo.MenuTree;
import com.wn.icloud.rest.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author leeyang
 * @create 2017-06-08 11:51
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController<UserBiz, User> {
    @Autowired
    private PermissionService permissionService;

    @Autowired
    private MenuBiz menuBiz;

    @Autowired
    private UserBiz userBiz;

    /**
     * 获取登陆用户信息 //todo 文档已记录
     *
     * @return
     */
    @GetMapping(value = "/front/info")
    public ResponseEntity<FrontUser> getUserInfo() {
        FrontUser userInfo = permissionService.getUserInfo(getCurrentUserName());
        return ResponseEntity.ok(userInfo);
    }

    /**
     * 获取用户菜单 //todo 文档已记录
     *
     * @return
     */
    @GetMapping(value = "/front/menus")
    public List<MenuTree> getMenusByUsername() {
        return permissionService.getMenusByUsername(getCurrentUserName());
    }

    /**
     * 获取所有菜单// todo 文档已记录
     *
     * @return
     */
    @RequestMapping(value = "/front/menu/all", method = RequestMethod.GET)
    public @ResponseBody
    List<Menu> getAllMenus() {
        return menuBiz.selectListAll();
    }
    // todo 文档已记录
    @PostMapping(value = "/resetPassword")
    public void resetPassword(@Valid @RequestBody PasswordResetDTO passwordResetDTO) {
        baseBiz.resetPassword(passwordResetDTO);
    }

    @PostMapping(value = "/updatePassword")
    public void updatePassword(@Valid @RequestBody PasswordResetDTO passwordResetDTO) {
        passwordResetDTO.setUserId(getCurrentUserId().intValue());
        baseBiz.updatePassword(passwordResetDTO);
    }

    @GetMapping(value = "/getUserByWorkTeam")
    public TableResultResponse<UserByWorkTeamReqDTO> getUserByWorkTeam(Long teamId, UserByWorkTeamReqDTO userByWorkTeamReqDTO, Integer page, Integer size){
        return userBiz.getUserByWorkTeam(teamId,userByWorkTeamReqDTO,page,size);

    }
}
